package com.tvshows.front.view.recap;

import java.util.Calendar;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Sash;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

import com.tvshows.front.ui.recap.TableCompositeRecapUI;
import com.tvshows.front.ui.refresh.RefreshUI;
import com.tvshows.front.view.labels.FontAssociation;
import com.tvshows.front.view.refresh.RefreshView;
import com.tvshows.interactions.ObserverBdd;

/**
 * @author tMidy
 */
public class TableCompositeRecapView
    extends Composite
    implements TableCompositeRecapUI
{

    private Table recapTable;

    private FormData refreshData;

    private FormData listData;

    /**
     * Constructor
     * 
     * @param parent composite parent
     * @param style style of the composite
     */
    public TableCompositeRecapView( Composite parent, int style )
    {
        super( parent, style );
        ObserverBdd.getObserverBdd().setTableRecap( this );
        this.setLayout( new FormLayout() );
        initFormData();
        initComposites();

    }

    /**
     * initializes the table
     */
    public void initTable()
    {
        recapTable = new Table( this, SWT.SINGLE | SWT.Selection );
        TableColumn nameRecapColumn = new TableColumn( recapTable, SWT.MULTI );
        nameRecapColumn.setText( "Nom de la série" );
        nameRecapColumn.setToolTipText( "Colonne nom" );
        nameRecapColumn.setWidth( 275 );
        nameRecapColumn.setResizable( false );

        TableColumn dateAddRecapColumn = new TableColumn( recapTable, SWT.MULTI );
        dateAddRecapColumn.setText( "date d'ajout de la série" );
        dateAddRecapColumn.setToolTipText( "Colonne date ajout" );
        dateAddRecapColumn.setWidth( 280 );
        dateAddRecapColumn.setResizable( false );

        recapTable.setHeaderVisible( true );
    }

    @Override
    public void addToTable( String linkName )
    {
        TableItem adderItem = new TableItem( recapTable, SWT.SINGLE );

        adderItem.setFont( FontAssociation.getFont( getDisplay() ) );
        adderItem.setText( new String[] { linkName, Calendar.getInstance().getTime().toString(), "Downloading..." } );
        recapTable.update();
        recapTable.layout();
        this.update();
        this.layout();
    }

    @Override
    public void deleteSelected()
    {
        if ( recapTable.getSelectionCount() != 0 )
        {
            recapTable.remove( recapTable.getSelectionIndex() );
            recapTable.select( recapTable.getSelectionCount() );
            this.layout();
        }
        recapTable.layout();
    }

    /**
     * @see com.tvshows.front.ui.FrontUI#initFormData()
     */
    public void initFormData()
    {
        Sash tableCompositeRecapSash = new Sash( this, SWT.HORIZONTAL );
        FormData sashData = new FormData();
        sashData.left = new FormAttachment( 0, 0 );
        sashData.bottom = new FormAttachment( 0, 0 );
        sashData.right = new FormAttachment( 100, 0 );

        refreshData = new FormData();
        refreshData.left = new FormAttachment( 0, 0 );
        refreshData.right = new FormAttachment( 100, 0 );
        refreshData.top = new FormAttachment( tableCompositeRecapSash, 0 );
        refreshData.bottom = new FormAttachment( 95, 0 );

        listData = new FormData();
        listData.left = new FormAttachment( 0, 0 );
        listData.right = new FormAttachment( 100, 0 );
        listData.bottom = new FormAttachment( 100, 0 );

        tableCompositeRecapSash.setLayoutData( sashData );
    }

    /**
     * @see com.tvshows.front.ui.FrontUI#initComposites()
     */
    public void initComposites()
    {
        initTable();
        this.recapTable.setLayoutData( refreshData );
        RefreshUI refreshView = new RefreshView( this );
        ( (RefreshView) refreshView ).setLayoutData( listData );
        refreshView.initComposites();
        this.layout();
    }

}
